<template>
  <div class="my-address">
    <ul class="addr-ul">
      <li v-for="(item,index) in address">
        <div class="li-left" @click="goByNow(index)">
          <div class="user-name">
            {{item.name}} <span>{{item.phone}}</span>
          </div>
          <div class="addr-box">

            <span v-if="item.defaults==1">默认</span>
            {{item.address}}{{item.addressDetails}}
          </div>
        </div>
        <div class="li-right">
          <router-link :to="'/addAddress/?from='+item.id">
            <span>编辑</span>
          </router-link>
        </div>
      </li>
    </ul>
    <router-link to="/addAddress/?from=0">
      <div class="add-btn">添加收货地址</div>
    </router-link>
  </div>
</template>

<script>
  import global from '../../public/global.vue'
  export default {
    name: 'myAddress',
    data() {
      return {
        address:[],
      }
    },
    methods: {

      goByNow(idx){
        let self = this;
        let type = this.$route.query.type;
        if(type=="back"){
          window.localStorage.setItem("address",self.address[idx].id);
          self.$router.go(-1)
        }



      }
    },
    created: function () {
      document.title = '我的收货地址';
      let self=this;
      this.$http.get(global.apiUrl+"/userAddress/getAddr" ).then((res) => {
        self.address=res.data.data;
      })
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="less" scoped>
  .my-address {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    min-height: 100%;
    background: #f3f3f3;
    .addr-ul {
      width: 100%;
      li {
        width: 100%;
        padding: 2% 2.5%;
        margin-bottom: 10px;
        background: #fff;
        display: flex;
        font-size: 1rem;
        color: #333;
      }
      .li-left {
        flex: 10;
        padding-right: 2%;
        border-right: 1px solid #eee;
        .user-name {
          span {
            font-size: 0.85rem;
            color: #666;
          }
        }
        .addr-box {
          font-size: 0.95rem;
          margin-top: 5px;
          span {
            font-size: 0.7rem;
            padding: 0 1%;
            border: 1px solid #d43f3f;
            color: #d43f3f;
            border-radius: 4px;
            margin-right: 5px;
          }
        }
      }
      .li-right {
        flex: 2;
        text-align: center;
        span {
          line-height: 3.5rem;
          font-size: 0.9rem;
          color: #666;
        }
      }
    }
    /*btn*/
    .add-btn {
      width: 90%;
      height: 2.5rem;
      line-height: 2.5rem;
      text-align: center;
      font-size: 1rem;
      color: #fff;
      margin: 50px auto 0;
      background: #d43f3f;
      border-radius: 6px;
    }
  }
</style>
